package scau.redi.social_platform.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import scau.redi.social_platform.dao.UserInfoMapper;
import scau.redi.social_platform.dao.UserRoleMapper;
import scau.redi.social_platform.domain.pojo.UserInfo;
import scau.redi.social_platform.domain.pojo.UserRole;
import scau.redi.social_platform.domain.vo.UserWithRoleVO;
import scau.redi.social_platform.service.LoginService;

import java.util.Date;

/**
 * @Author: Redi
 * @Date: 2022/5/8 16:56
 * @Description:
 */
@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    UserInfoMapper userInfoMapper;
    @Autowired
    UserRoleMapper userRoleMapper;

    @Override
    public UserInfo findByUsername(String username) {
        return userInfoMapper.selectByUsername(username);
    }

    @Override
    public UserInfo loginByUsernameAndPassword(String username, String password) {
        return userInfoMapper.selectByUsernameAndPassword(username, password);
    }

    @Override
    public UserWithRoleVO findUserWithRole(String username) {
        UserWithRoleVO vo = new UserWithRoleVO();
        UserInfo userInfo = userInfoMapper.selectByUsername(username);
        UserRole userRole = userRoleMapper.selectByPrimaryKey(userInfo.getRoleId());

        vo.setUsername(username);
        vo.setPassword(userInfo.getPassword());
        vo.setRoleId(userRole.getRoleId());
        vo.setRoleName(userRole.getRoleName());
        vo.setStatusId(userInfo.getStatusId());
        vo.setCreateTime(new Date());
        vo.setUpdateTime(new Date());

        return vo;
    }

    @Override
    public int registerByUsernameAndPassword(UserInfo userInfo) {
        if (userInfoMapper.insertSelective(userInfo)!=0){
            return userInfoMapper.selectLastInsertId();
        }
        return 0;
    }

    @Override
    public UserInfo findByUserId(int userId) {
        return userInfoMapper.selectByPrimaryKey(userId);
    }
}
